<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" v-if="props.search" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        v-model:selections="selections"
        border
        stripe
        row-key="id"
        row-click-checked
      >
        <template #rebate="{ row }"> {{ row.rebate }}%</template>
        <template #insurance_salesman="{ row }">
          <span>{{ row.insurance_salesman }}</span>
        </template>
        <template #insurance="{ row }">
          {{ row.insurance }}
        </template>

        <template #totalPrice="{ row }">
          <span :class="priceClass(row.raise_price)">{{ row.totalPrice }}</span>
        </template>

        <template #otherPrice="{ row }">
          {{ row.otherPrice }}
        </template>
        <template #s_time="{ row }">
          {{ row.sTime }}
        </template>
        <template #e_time="{ row }">
          {{ row.eTime }}
        </template>
        <template #create="{ row }">
          {{ row.create_user }}
          <br />
          {{ row.createDate }}
        </template>
        <template #apply="{ row }">
          {{ row.apply_user }}
          <br />
          {{ row.applyTime }}
        </template>

        <template #action="{ row }">
          <el-button-group>
            <el-button @click="openImages(row)" size="small" type="primary">
              图
            </el-button>
            <el-button @click="openEdit(row)" size="small" type="warning">
              编
            </el-button>
            <el-button @click="handleDelete(row)" size="small" type="danger">
              删
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
    </ele-card>
    <!-- 图片 -->
    <images v-model="showImages" :data="current" :read-only="true" />
    <!-- 编辑报价 -->
    <create
      v-model="showCreate"
      :data="current"
      :plate-number="plateNumber"
      @done="reload"
    />
  </ele-page>
</template>

<script setup>
  import { ref, watch, computed } from 'vue';
  import { EleMessage } from 'ele-admin-plus/es';
  import { ElMessageBox } from 'element-plus/es';
  import { delInsurance, pageInsuranceOffer } from '@/api/company';
  import Create from '@/views/insurance/components/create.vue';

  import Search from './components/search.vue';
  import Images from './components/images.vue';
  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    data: Object,
    cate: String,
    vehicleId: {
      type: Number,
      default: 0
    },
    search: {
      type: Boolean,
      default: true
    }
  });
  const title = ref('');
  /** 表格实例 */
  const tableRef = ref(null);

  const priceClass = (value) => {
    return value > 0 ? 'red' : 'blue';
  };

  /** 表格列配置 */
  const columns = computed(() => {
    const cols = [];
    if (props.action === 'choose') {
      cols.push({
        type: 'selection',
        columnKey: 'selection',
        width: 50,
        align: 'center',
        fixed: 'left'
      });
    }

    cols.push(
      {
        width: 80,
        prop: 'id',
        label: '编号'
      },
      {
        width: 100,
        prop: 'cate',
        label: '类型'
      },
      {
        width: 120,
        prop: 'plate_number',
        label: '车牌号码'
      },
      {
        width: 150,
        prop: 'insurance',
        label: '保险公司'
      },
      {
        slot: 'insurance_salesman',
        label: '保险业务员',
        width: 120
      },
      {
        width: 80,
        slot: 'rebate',
        label: '返点'
      },
      {
        width: 100,
        slot: 'totalPrice',
        label: '总价'
      },
      {
        width: 100,
        prop: 'price',
        label: '价格'
      },
      {
        width: 100,
        slot: 'otherPrice',
        label: '加价/车船'
      },
      {
        width: 170,
        slot: 's_time',
        align: 'center',
        label: '开始时间'
      },
      {
        width: 170,
        slot: 'e_time',
        align: 'center',
        label: '结束日期'
      },
      {
        width: 150,
        slot: 'create',
        align: 'center',
        label: '报价时间'
      },
      {
        width: 150,
        slot: 'apply',
        align: 'center',
        label: '申请人'
      },
      {
        slot: 'action',
        label: '操作'
      }
    );

    return cols;
  });

  /** 表格选中数据 */
  const selections = ref([]);
  const current = ref([]);
  /** 表格数据源 */

  const datasource = async ({ page, limit, where, orders }) => {
    const result = await pageInsuranceOffer({
      ...where,
      ...orders,
      page,
      limit,
      vehicle_id: props.vehicleId
    });
    return result;
  };

  /** 报价 */
  const plateNumber = ref('');
  const showCreate = ref(false);
  const openEdit = (row) => {
    current.value = row;
    plateNumber.value = row.plate_number;
    showCreate.value = true;
  };

  // 审核
  const handleDelete = (row) => {
    ElMessageBox.confirm(
      '确定要删除' +
        row.plate_number +
        row.insurance +
        row.price +
        '的报价单吗？',
      '系统提示',
      {
        type: 'warning',
        draggable: true
      }
    )
      .then(() => {
        const loading = EleMessage.loading('请求中..');
        delInsurance({ id: row.id, vehicle_id: row.vehicle_id })
          .then(() => {
            loading.close();
            EleMessage.success('操作成功');
            reload();
          })
          .catch((e) => {
            loading.close();
          });
      })
      .catch(() => {});
  };

  /** 查看图片 */
  const showImages = ref(false);
  const openImages = (data) => {
    current.value = data;
    showImages.value = true;
  };
  /** 搜索 */
  const reload = (where) => {
    tableRef.value?.reload?.({ page: 1, where });
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.data.plate_number + '的保险列表';
      } else {
        console.log('关闭');
      }
    }
  );
</script>
